using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using Sedna.API.Support;

namespace Sedna.API.Security
{
    [Serializable]
    public class Token: IToken
    {
        private byte[] value;


        public byte[] Value
        {
            get { return value; }
            set { this.value = value; }
        }

        public Token(string base64value)
        {
            this.value = Convert.FromBase64String(base64value);
        }

        public Token(byte[] value)
        {
            this.value = value;
        }

        public override string ToString()
        {
            return Convert.ToBase64String(value);
        }


        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            Token token = obj as Token;
            if (token == null) return false;
            if (token.Value == null && Value==null) return true;
            if (token.Value == null ^ Value ==null) return false;
            if (token.Value.Length != Value.Length) return false;
            for (int i=0; i<Value.Length; i++)
            {
                if (token.Value[i] != Value[i]) return false;        
            }
            return true;
        }


        public override int GetHashCode()
        {
            if (Value == null) return 0;
            return ToString().GetHashCode();
        }
    }
}

